package com.glc.hms.system.controller.api;

import cn.hutool.core.util.IdUtil;
import com.glc.hms.common.controller.BaseController;
import com.glc.hms.common.data.ErrorResponseData;
import com.glc.hms.common.data.ResponseData;
import com.glc.hms.common.data.SuccessResponseData;
import com.glc.hms.common.util.Assert;
import com.glc.hms.core.annotation.AuthIgnore;
import com.glc.hms.system.model.info.LoginSuccessInfo;
import com.glc.hms.system.model.vo.UserLoginVO;
import com.glc.hms.system.service.ISysUserService;
import com.glc.hms.system.service.ISysUserTokenService;
import com.google.common.collect.Maps;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @author Santa
 * @since 2019/11/14
 */
@Slf4j
@RestController
@RequestMapping("/api")
public class ApiLoginController extends BaseController {
    @Autowired
    private ISysUserService userService;
    @Autowired
    private ISysUserTokenService sysUserTokenService;

    /**
     * 登录
     */
    @AuthIgnore
    @GetMapping("login")
    @ApiOperation(value="登录", notes="登录")
    public ResponseData login(String loginName, String password){
        Assert.isBlank(loginName, "手机号不能为空");
        Assert.isBlank(password, "密码不能为空");

        // 查询用户信息
        //UserLoginVO login = userService.login(loginName, password);
        UserLoginVO login = null;
        if (login == null){
            return new ErrorResponseData("未查到用户信息!");
        }
        String webSessionId = IdUtil.simpleUUID();
        Map<String, Object> map1 = sysUserTokenService.creatToken(login.getUserId());

        //需要返回的用户信息,需修改，直接返回实体对象
        LoginSuccessInfo successInfo = new LoginSuccessInfo();
        successInfo.setUserId(login.getUserId());
        successInfo.setUserName(login.getUsername());
        successInfo.setUserMobile(login.getMobile());
        successInfo.setRoleId(login.getDeptId());
        Map map= Maps.newHashMap();
        map.put("sessionId",webSessionId);
        map.put("successInfo",successInfo);
        map.putAll(map1);
        return new SuccessResponseData(map);
    }
}























